package com.candan.fy.hdf.sub;

import java.io.File;
import java.io.IOException;

import com.esri.arcgis.datasourcesraster.IPixelBlock3;
import com.esri.arcgis.datasourcesraster.IPixelBlock3Proxy;
import com.esri.arcgis.datasourcesraster.IRasterBand;
import com.esri.arcgis.datasourcesraster.IRasterBandCollection;
import com.esri.arcgis.datasourcesraster.IRasterEdit;
import com.esri.arcgis.datasourcesraster.IRasterProps;
import com.esri.arcgis.datasourcesraster.IRasterPropsProxy;
import com.esri.arcgis.datasourcesraster.RasterWorkspaceFactory;
import com.esri.arcgis.geodatabase.IPixelBlockProxy;
import com.esri.arcgis.geodatabase.IPnt;
import com.esri.arcgis.geodatabase.IRaster;
import com.esri.arcgis.geodatabase.IRasterDataset;
import com.esri.arcgis.geodatabase.IRasterWorkspace2;
import com.esri.arcgis.geodatabase.IRasterWorkspace2Proxy;
import com.esri.arcgis.geodatabase.IWorkspaceFactory;
import com.esri.arcgis.geodatabase.Pnt;
import com.esri.arcgis.geodatabase.rstPixelType;
import com.esri.arcgis.geometry.IGeographicCoordinateSystem;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriSRGeoCSType;
import com.esri.arcgis.geoprocessing.GeoProcessor;
import com.esri.arcgis.geoprocessing.tools.analyst3dtools.Reclassify;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.interop.Cleaner;

public class RasterOperate {

	private int width;
	private int height;
	private double xCell;
	private double yCell;
	private float xMin;
	private float xMax;
	private float yMin;
	private float yMax;

	private float maxvalue;
	private float minvalue;
	
	public float getMaxvalue() {
		return maxvalue;
	}
	public void setMaxvalue(float maxvalue) {
		this.maxvalue = maxvalue;
	}
	public float getMinvalue() {
		return minvalue;
	}
	public void setMinvalue(float minvalue) {
		this.minvalue = minvalue;
	}
	public RasterOperate(){
		width = Integer.parseInt(PropertyParse.getValue("target.raster.width"));
		height = Integer.parseInt(PropertyParse.getValue("target.raster.height"));
		xCell = Double.parseDouble(PropertyParse.getValue("target.raster.xCell"));
		yCell = Double.parseDouble(PropertyParse.getValue("target.raster.yCell"));
		xMin = Float.parseFloat(PropertyParse.getValue("target.raster.xMin"));
		xMax = Float.parseFloat(PropertyParse.getValue("target.raster.xMax"));
		yMin = Float.parseFloat(PropertyParse.getValue("target.raster.yMin"));
		yMax = Float.parseFloat(PropertyParse.getValue("target.raster.yMax"));
	}
	//This example creates a raster dataset with a specified dimension, populates pixel values, and set a NoData value.
	public IRasterDataset createRasterDataset3(String file,float[][] data)throws
    Exception
{
	File f = new File(file);
	//Create raster workspace. This example also works with any other workspaces that support raster data such as a file geodatabase (FGDB) workspace.
	//Access the workspace and the SDE workspace.
	IRasterWorkspace2 rasterWs = openRasterWorkspace(f.getParent());
	//Define the spatial reference of the raster dataset.
	SpatialReferenceEnvironment spatialReferenceEnvironment = new SpatialReferenceEnvironment();
	IGeographicCoordinateSystem sr = spatialReferenceEnvironment.createGeographicCoordinateSystem(4326);//esriSRGeoCSType.esriSRGeoCS_WGS1984);

	//Define the origin for the raster dataset, which is the lower left corner of the raster.
	IPoint origin = new Point();
	origin.putCoords(xMin, yMin);
//	//Define the dimension of the raster dataset.
//	int width = 3000; //This is the width of the raster dataset.
//	int height = 3000; //This is the height of the raster dataset.
//	double xCell = 0.04; //This is the cell size in x direction.
//	double yCell = 0.04; //This is the cell size in y direction.
	int NumBand = 1; // This is the number of bands the raster dataset contains.
	//Create a raster dataset in grid format.
	IRasterDataset rasterDataset = rasterWs.createRasterDataset(f.getName(), "TIFF",
		origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_FLOAT, sr,
		true);
	//Get the raster band.
	IRasterBandCollection rasterBands = (IRasterBandCollection)(rasterDataset);
	IRasterBand rasterBand;
	IRasterProps rasterProps;
	rasterBand = rasterBands.item(0);
	rasterProps = new IRasterPropsProxy(rasterBand);
	//Set NoData if necessary. For a multiband image, NoData value needs to be set for each band.
	rasterProps.setNoDataValue(999.0);
	//Create a raster from the dataset.
	IRaster raster = rasterDataset.createDefaultRaster();

	//Create a pixel block.
	IPnt blocksize = new Pnt();
	blocksize.setCoords(width, height);
	IPixelBlock3 pixelblock = new IPixelBlock3Proxy(raster.createPixelBlock
		(blocksize));

	//Populate some pixel values to the pixel block.
	float[][] pixels = (float[][])pixelblock.getPixelData(0);
	for (int i = 0; i < width; i++)
		for (int j = 0; j < height; j++){
			if(data[i][j] > this.getMaxvalue() || data[i][j] < this.getMinvalue())pixels[i][j] = 0;
			else pixels[i][j] = data[i][j];
			
		}

	pixelblock.setPixelData(0, pixels);

	//Define the location that the upper left corner of the pixel block is to write.
	IPnt upperLeft = new Pnt();
	upperLeft.setCoords(xMin,yMax);

	//Write the pixel block.
	IRasterEdit rasterEdit = (IRasterEdit)(raster);
	rasterEdit.write(upperLeft, new IPixelBlockProxy(pixelblock));

	//Release rasterEdit explicitly.
	Cleaner.release(rasterEdit);
	return rasterDataset;
}
	
	static IRasterWorkspace2 openRasterWorkspace(String path)throws Exception{
	    //This function opens a raster workspace.
	    IWorkspaceFactory workspaceFact = new RasterWorkspaceFactory();
	    return new IRasterWorkspace2Proxy(workspaceFact.openFromFile(path, 0));
	}


	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
}

}
